<template>
  <div class="li-list" ref="wrapper">
    <div>
      <div class="area">
        <div class="li-title border-topbottom">当前城市</div>
        <div class="li-button-list">
          <div class="li-button-wrapper">
            <div class="li-button">{{this.currentCity}}</div>
          </div>
        </div>
      </div>

      <!--热门城市-->
      <div class="area">
        <div class="li-title border-topbottom">热门城市</div>
        <div class="li-button-list">
          <div
            class="li-button-wrapper"
            v-for="(item) in hotCities"
            :key="item.id"
            @click="handleCityClick(item.name)"
          >
            <div class="li-button">{{item.name}}</div>
          </div>
        </div>
      </div>

      <!--字母搜索城市列表-->
      <!--cities{key是所在字母，value是字母对应的各个城市}
        item{id:""，spell:"",name:""}
      -->
      <div class="area" v-for="(item, key) in cities" :key="key" :ref="key">
        <div class="li-title border-topbottom">{{key}}</div>
        <div class="li-item-list">
          <div
            class="li-item border-bottom"
            v-for="(innerItem) in item"
            :key="innerItem.id"
            @click="handleCityClick(innerItem.name)"
          >{{innerItem.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapActions } from 'vuex'
export default {
  name: 'CityList',
  props: {
    hotCities: Array,
    // cities是个对象，对面里面有A,B,C...这些对象（key--value的形式)，A对象里面又是数组
    cities: Object
  },
  data: function () {
    return {
      letter: ''
    }
  },
  computed: {
    ...mapState({
      // 将state.js里面的city映射的名字叫currentCity
      currentCity: 'city'
    })
  },
  methods: {
    handleCityClick: function (cityName) {
      // 在组件里面通过dispatch函数来调用Actions, 这个aciton叫做changeCity
      // this.$store.dispatch('changeCity', cityName)
      this.changeCity(cityName)
      // 我们使用router，每个vue实例组件都有$router属性
      this.$router.push('/')
    },
    // 使用maoActions隐藏了dispatch
    ...mapActions(['changeCity'])
  },
  mounted: function () {
    var this_ = this
    // 要使用better-scroll的话，必须获取城市列表的dom
    this.scroll = new Bscroll(this.$refs.wrapper)
    // 监听到这个change事件，msg是传过来的事件对象
    this_.bus.$on('change', msg => {
      this.letter = msg
      // this.letter里面有数据就打印，没有就不打印
      if (this.letter) {
        // 对应当前的dom对象
        const element = this.$refs[this.letter][0]
        // 让滚动区域滚动到dom对象上
        this.scroll.scrollToElement(element)
      }
    })
  }

}
</script>

<style lang="stylus" scoped>
@import '~@/assets/styles/varibles.styl';

.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.li-list {
  position: absolute;
  top: 1.58rem;
  left: 0;
  right: 0;
  bottom: 0;
  overflow: hidden;

  .border-bottom {
    &:before {
      border-color: #ccc;
    }
  }

  .li-title {
    height: 0.5rem;
    line-height: 0.5rem;
    background: #eeeeee;
    padding-left: 0.2rem;
    color: #666;
    font-size: 0.26rem;
  }

  .li-button-list {
    /* 触发bfc */
    overflow: hidden;
    padding: 0.1rem;
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;

    .li-button-wrapper {
      float: left;
      width: 33.33%;

      .li-button {
        margin: 0.1rem;
        padding: 0.1rem 0;
        text-align: center;
        border: 0.04rem solid #ccc;
        border-radius: 0.06rem;
      }
    }
  }

  .li-item-list {
    .li-item {
      line-height: 0.56rem;
      color: #666;
      padding-left: 0.2rem;
    }
  }
}
</style>
